package minh.bac.wcbrazil2014.favorite;

import minh.bac.wcbrazil2014.R;
import minh.bac.wcbrazil2014.WCTAG;
import minh.bac.wcbrazil2014.database.MySQLiteManager;
import minh.bac.wcbrazil2014.listviewmatch.DeTailTeamFM;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

public class FavoriteChildFM extends Fragment{

	private MySQLiteManager mMySQLiteManager;
	boolean clickThongtin;
	static FragmentManager fragmentManager;
	private String nameTeam, imgLink;
	public static FavoriteChildFM newInstance(String nameTeam, String imgLink)
	{
		FavoriteChildFM childFM = new FavoriteChildFM();
		Bundle bundle = new Bundle();
		bundle.putString(WCTAG.KEY_NAMETEAM, nameTeam);
		bundle.putString(WCTAG.KEY_IMGLINK_TEAM, imgLink);
		childFM.setArguments(bundle);
		return childFM;				
	}
	TextView thongtin, lichdau;
	ImageButton imgBtnBack;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.favorite_fmchild, container, false);
		mMySQLiteManager = new MySQLiteManager(getActivity());
		thongtin = (TextView) rootView.findViewById(R.id.text_thongtins);
		lichdau = (TextView) rootView.findViewById(R.id.text_lichdau);
		imgBtnBack = (ImageButton) rootView.findViewById(R.id.favoritebutton_back);
		//
		nameTeam = getArguments().getString(WCTAG.KEY_NAMETEAM);
		imgLink = getArguments().getString(WCTAG.KEY_IMGLINK_TEAM);
		//
		fragmentManager = getChildFragmentManager();
		clickThongtin = true;
		//
		if(clickThongtin )
		{
			thongtin.setBackgroundColor(getActivity().getResources().getColor(R.color.blue));
			thongtin.setTextColor(getActivity().getResources().getColor(R.color.white));
			switchFragment(R.id.container_favorite_items, FavoriteFBDTFM.newInstance(nameTeam, imgLink), true, null);
		}
		thongtin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(!clickThongtin)
				{
					clickThongtin = true;
					lichdau.setBackgroundColor(getActivity().getResources().getColor(R.color.white));
					lichdau.setTextColor(getActivity().getResources().getColor(R.color.blue));
					
					thongtin.setBackgroundColor(getActivity().getResources().getColor(R.color.blue));
					thongtin.setTextColor(getActivity().getResources().getColor(R.color.white));
					switchFragment(R.id.container_favorite_items, FavoriteFBDTFM.newInstance(nameTeam, imgLink), true, null);
				
				}
			}
		});
		lichdau.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				clickThongtin = false;
				thongtin.setBackgroundColor(getActivity().getResources().getColor(R.color.white));
				thongtin.setTextColor(getActivity().getResources().getColor(R.color.blue));
				
				lichdau.setBackgroundColor(getActivity().getResources().getColor(R.color.blue));
				lichdau.setTextColor(getActivity().getResources().getColor(R.color.white));
				switchFragment(R.id.container_favorite_items, ListViewChildFM.newInstance(nameTeam, imgLink), true, null);
			
			}
		});
		imgBtnBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				FavoriteFragment.switchFragment(R.id.container_favorite, new FavoriteFragment(), false, null);
			}
		});
		return rootView;
	}
	public static void switchFragment(int idContainer, Fragment fragment ,boolean addToBackStack, String TAG)
	{
		if(addToBackStack)
		{
			if( TAG!= null )
			{
				fragmentManager.beginTransaction().replace(idContainer, fragment, TAG).addToBackStack(TAG).commit();
				
			}
			else
			{
				fragmentManager.beginTransaction().replace(idContainer, fragment).addToBackStack(null).commit();
			}
		}
		else
		{
			if( TAG!= null )
			{
				fragmentManager.beginTransaction().replace(idContainer, fragment, TAG).commit();
			}
			else
			{
				fragmentManager.beginTransaction().replace(idContainer, fragment).commit();
			}
		}
	}
	
}
